<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Atom_Entry_Content implements Atom_ChildElement {
    
    private $content;
    private $type;
    
    const ELEMENT_CONTENT = "content";
    const TYPE_TEXTHTML = "text/html";
    const TYPE_HTML = "html";
    
    public function __construct($content, $type) {
        $this->setContent($content);
        $this->setType($type);
    }
    
    private function setContent($content) {
        if(empty($content)) {
            throw new Exception(__CLASS__ . " - Content cannot be set empty.");
        }
        //$replaceThese = array("&ldquo;", "&rdquo;", "&mdash;", "&rsquo;", "&ndash;", "&eacute;");
        //$this->content = htmlentities($content, ENT_QUOTES, "UTF-8", false);
        //$this->content = str_replace($replaceThese, "", $this->content);
        $this->content = htmlspecialchars($content, ENT_QUOTES, "UTF-8", false);
    }
    
    public function getContent() {
        return $this->content;
    }
    
    private function setType($type) {
        if(empty($type)) {
            //throw new Exception("Type cannot be set empty.");
            $type = self::TYPE_HTML;
        }
        $this->type = $type;
    }
    
	/**
     * @return DOMElement
     */
    public function asDomElement(DOMDocument $dom) {
        $element = $dom->createElement(self::ELEMENT_CONTENT, $this->content);
        $element->setAttribute("type", $this->type);
        return $element;
    }
    
	/**
     * @param DOMElement $elm
     * @return Atom_Entry_Content
     */
    public static function buildFromDOMElement(DOMElement $elm) {
        return new Atom_Entry_Content($elm->nodeValue, $elm->getAttribute('type'));
    }
}